﻿using System;
using System.Linq;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

using WoChu.RfClient.Entity;
using WoChu.RfClient.Common;
using WoChu.CF.BaseFrame.Util;

namespace WoChu.RfClient.Duty
{
    public partial class FrmPick : Form
    {
        public FrmPick()
        {
            InitializeComponent();
        }

        private static PickInfo oPickInfo = null;
        private static List<PickItemInfo> lstPickItem = null;
        private static Dictionary<string, PickItemInfo> dictPickItem = null;
        private static int iSort = 0;

        private void FrmPick_Load(object sender, EventArgs e)
        {
            //获取
            List<string> _lstWarehouseName = Duty.DutyUIRule.NewInstance.GetPDAWarehouseName(LoginBean.Instance.USERCODE).DATA;
            cmbWarehouse.Items.Clear();
            foreach (string _item in _lstWarehouseName)
            {
                cmbWarehouse.Items.Add(_item);
            }

            List<string> _lstBatchNo = Duty.DutyUIRule.NewInstance.GetPDAPickBatchNo(LoginBean.Instance.USERCODE).DATA;
            cmbBatchNo.Items.Clear();
            foreach (string _item in _lstBatchNo)
            {
                cmbBatchNo.Items.Add(_item);
            }


            this.dgGridPick.DataSource = this.pickSource;
        }

        private void btnPick_Click(object sender, EventArgs e)
        {
            if (string.IsNullOrEmpty(cmbWarehouse.Text.Trim()))
            {
                MessageBoxEx.Show("请先选择库位!");
                cmbWarehouse.Focus();
                return;
            }

            if (string.IsNullOrEmpty(cmbBatchNo.Text.Trim()))
            {
                MessageBoxEx.Show("请先选择批次!");
                cmbWarehouse.Focus();
                return;
            }

            //提示是否重新获取任务
            if (oPickInfo != null && oPickInfo.PDAPICKID > 0 && MessageBoxEx.Question("确定重新获取任务吗？未提交的数据将丢失！") != DialogResult.Yes)
            {
                return;
            }

            try
            {
                Cursor.Current = Cursors.WaitCursor;
                iSort = 0;

                oPickInfo = Duty.DutyUIRule.NewInstance.GetPDAPickInfo(cmbBatchNo.Text.Trim(), cmbWarehouse.Text.Trim(), LoginBean.Instance.USERCODE).DATA;

                if (oPickInfo != null && oPickInfo.PDAPICKID > 0)
                {
                    lstPickItem = Duty.DutyUIRule.NewInstance.GetPDAPickItemInfo(oPickInfo.PDAPICKID.ToString()).DATA;

                    dictPickItem = new Dictionary<string, PickItemInfo>();

                    foreach (var _item in lstPickItem)
                    {
                        dictPickItem.Add(_item.GOODSCODE, _item);
                        _item.DISTRQTY = iSort++;
                    }

                    this.pickSource.DataSource = lstPickItem;
                    this.dgGridPick.DataSource = this.pickSource;

                    lblBatchDate.Text = oPickInfo.BATCHDATE;
                    txtProdID.Text = string.Empty;
                    txtProdID.Focus();

                }
                else
                {
                    WoChu.CF.BaseFrame.Util.MessageBoxEx.Show(cmbWarehouse.Text + "库位没有分拣的任务！");
                }
            }
            catch (Exception ex)
            {
                WoChu.CF.BaseFrame.Util.MessageBoxEx.Show(ex);
            }
            finally
            {
                Cursor.Current = Cursors.Default;
            }

        }

        private void btnSubmit_Click(object sender, EventArgs e)
        {
            try
            {
                if (lstPickItem == null || lstPickItem.Count <= 0) return;
                List<PickItemInfo> _PickItems = lstPickItem.FindAll(a => a.REALQTY > 0);

                if (_PickItems != null && _PickItems.Count < lstPickItem.Count)
                {
                    if (MessageBoxEx.Question("数据未完成，确定提交数据吗？") != DialogResult.Yes) return;
                }

                if (oPickInfo != null && oPickInfo.PDAPICKID > 0)
                {
                    Cursor.Current = Cursors.WaitCursor;

                    bool _result = Duty.DutyUIRule.NewInstance.SubmitPDAPick(oPickInfo.PDAPICKID.ToString(),
                                     oPickInfo.BATCHDATE, oPickInfo.STATE, LoginBean.Instance.USERCODE, lstPickItem).DATA;

                    if (_result)
                    {
                        //清空数据
                        oPickInfo = null;
                        lstPickItem.Clear();
                        lstPickItem = null;
                        dictPickItem.Clear();
                        dictPickItem = null;

                        lblBatchDate.Text = string.Empty;
                        txtProdID.Text = string.Empty;
                        this.pickSource.DataSource = null;
                        this.dgGridPick.DataSource = null;

                        cmbWarehouse.Focus();
                    }
                    else
                    {
                        WoChu.CF.BaseFrame.Util.MessageBoxEx.Show("提交失败！");
                    }
                }
            }
            catch (Exception ex)
            {
                WoChu.CF.BaseFrame.Util.MessageBoxEx.Show(ex); 
            }
            finally
            { 
                Cursor.Current = Cursors.Default; 
            }
        }


        private void cmbWarehouse_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode != Keys.Enter) return;

            btnPick_Click(sender, e);

        }

        private void txtProdID_KeyDown(object sender, KeyEventArgs e)
        {
            try
            {
                if (e.KeyCode != Keys.Enter) return;

                if (lstPickItem == null || lstPickItem.Count <= 0) return;

                string _prodID = this.txtProdID.Text.Trim();

                if (!string.IsNullOrEmpty(_prodID))
                {
                    PickItemInfo _pickItemInfo = null;
                    try
                    {
                        _pickItemInfo = dictPickItem[_prodID];
                    }
                    catch
                    {
                        WoChu.CF.BaseFrame.Util.MessageBoxEx.Show("扫描的商品不正确！");
                        txtProdID.Text = string.Empty;
                        txtProdID.Focus();
                        return;
                    }

                    if (_pickItemInfo.REALQTY > 0)
                    {
                        WoChu.CF.BaseFrame.Util.MessageBoxEx.Show("已经扫描过该商品！");
                    }
                    else
                    {
                        _pickItemInfo.REALQTY = _pickItemInfo.QTY;
                    }

                    _pickItemInfo.DISTRQTY = -1;
                    lstPickItem.Sort((x, y) =>
                    {
                        return x.DISTRQTY.CompareTo(y.DISTRQTY);
                    });

                    dgGridPick.Refresh();

                    //设置已扫描的为加亮
                    SelectionBright();

                    //当中序列号
                    _pickItemInfo.DISTRQTY = iSort++;
                    txtProdID.Text = string.Empty;
                    txtProdID.Focus();

                    //自动提交数据
                    List<PickItemInfo> _PickItems = lstPickItem.FindAll(o => o.REALQTY > 0 && o.REALQTY==o.QTY);
                    if (_PickItems != null && _PickItems.Count == lstPickItem.Count)
                    {
                        btnSubmit_Click(sender, e);
                        WoChu.CF.BaseFrame.Util.MessageBoxEx.Show("商品分拣完成，数据已经提交！");

                    }
                }
            }
            catch (Exception ex)
            { WoChu.CF.BaseFrame.Util.MessageBoxEx.Show(ex); }
         
        }

        private void txtRealQty_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode != Keys.Enter) return;

            string _realQty = this.txtRealQty.Text.Trim();

            if (!string.IsNullOrEmpty(_realQty))
            {
                decimal _Qty = 0;
                try
                {                    
                    _Qty = Convert.ToDecimal(_realQty);
                }
                catch
                {
                    WoChu.CF.BaseFrame.Util.MessageBoxEx.Show("输入的数量不正确!");
                    txtRealQty.Text = string.Empty;
                    txtRealQty.Focus();
                    return;
                }
                //修改实拣量
                PickItemInfo _pickItemInfo = lstPickItem[dgGridPick.CurrentCell.RowNumber];
                _pickItemInfo.REALQTY = _Qty;

                dgGridPick.Refresh();
                //设置已扫描的为加亮
                SelectionBright();

                txtRealQty.Text = string.Empty;
                txtProdID.Text = string.Empty;
                txtProdID.Focus();
            }
        }

        private void SelectionBright()
        {
            if (lstPickItem == null || lstPickItem.Count <= 0) return;
            //设置已扫描的为加亮
            List<PickItemInfo> _PickItems = lstPickItem.FindAll(o => o.REALQTY > 0);
            foreach (var _item in _PickItems)
            {
                dgGridPick.Select(lstPickItem.IndexOf(_item));

                dgGridPick.SelectionBackColor = Color.Green;
              
            }
        }

        private void dgGridPick_DoubleClick(object sender, EventArgs e)
        {
            if (lstPickItem != null && lstPickItem.Count > 0)
            {
                PickItemInfo _pickItemInfo = lstPickItem[dgGridPick.CurrentRowIndex];
                if (_pickItemInfo != null)
                {
                    this.txtProdID.Text = _pickItemInfo.GOODSCODE;
                    this.txtRealQty.Text = string.Empty;
                    this.txtProdID.Focus();
                }
            }
        }

        private void FrmPick_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Escape)
            {
                if (lstPickItem != null && lstPickItem.Count > 0)
                {
                    List<PickItemInfo> _PickItems = lstPickItem.FindAll(a => a.REALQTY > 0);

                    if (_PickItems != null && _PickItems.Count > 0)
                    {
                        if (MessageBoxEx.Question("确定退出吗？未提交的数据将丢失！") != DialogResult.Yes) return;
                    }
                }

                //System.Reflection.Assembly ass = System.Reflection.Assembly.Load("WoChu.RfClient.Login");
                //Type type = ass.GetType("WoChu.RfClient.Login.FrmMain");
                //Object o = Activator.CreateInstance(type);
                //System.Reflection.MethodInfo mi = type.GetMethod("Show");
                //mi.Invoke(o, null);
                this.Close();
                this.Dispose();
            }
        }

        private void FrmPick_Closed(object sender, EventArgs e)
        {
            FrmPick_KeyDown(sender, new KeyEventArgs(Keys.Escape));
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            try
            {
                Cursor.Current = Cursors.WaitCursor;
                
                if (oPickInfo == null || oPickInfo.PDAPICKID <= 0) return;
                if (lstPickItem == null || lstPickItem.Count <= 0) return;
                List<PickItemInfo> _PickItems = lstPickItem.FindAll(a => a.REALQTY > 0);
               
                bool _result = Duty.DutyUIRule.NewInstance.SavePDAPick(LoginBean.Instance.USERCODE, _PickItems).DATA;

                if (_result)
                {
                    //清空数据
                    oPickInfo = null;
                    lstPickItem.Clear();
                    lstPickItem = null;
                    dictPickItem.Clear();
                    dictPickItem = null;

                    lblBatchDate.Text = string.Empty;
                    txtProdID.Text = string.Empty;
                    this.pickSource.DataSource = null;
                    this.dgGridPick.DataSource = null;
                    WoChu.CF.BaseFrame.Util.MessageBoxEx.Show("保存成功！");

                    cmbWarehouse.Focus();
                }
                else
                {
                    WoChu.CF.BaseFrame.Util.MessageBoxEx.Show("保存失败！");
                }
            }
            catch (Exception ex)
            {
                WoChu.CF.BaseFrame.Util.MessageBoxEx.Show(ex);
            }
            finally
            {
                Cursor.Current = Cursors.Default;
            }
        }

       

    }
}